package com.xyy.saas.payment.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.api.YunZhangHuApi;
import com.xyy.saas.payment.cores.bo.YzhBankPayBo;
import com.xyy.saas.payment.cores.bo.YzhwxPayBo;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.cores.vo.ResultVO;
import com.xyy.saas.payment.cores.vo.YzhBankPayVo;
import com.xyy.saas.payment.cores.vo.YzhVerifyBankcardVo;
import com.xyy.saas.payment.cores.vo.YzhwxPayVo;
import com.xyy.saas.payment.manager.BankPayManager;
import com.xyy.saas.payment.manager.WXPayManager;
import com.xyy.saas.payment.util.JSONUtils;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * @Author lina
 * @Description
 * @Date 2022/3/3
 **/
@Service(version = "1.0.0")
@Slf4j
public class YunZhangHuApiImpl implements YunZhangHuApi {
    @Resource
    private BankPayManager bankPayManager;
    @Resource
    private WXPayManager wxPayManager;


    @Override
    public ResultVO verifyBankcard(YzhVerifyBankcardVo verifyBankcardVo, String sign) {
        try {
            return bankPayManager.verifyBankCard(verifyBankcardVo);
        } catch (Exception e) {
            log.error("verifyBankcard:异常 param {}" , verifyBankcardVo, e);
            return ResultVO.createError("系统异常");
        }
    }

    @Override
    public ResultVO<YzhBankPayBo> bankPay(YzhBankPayVo bankPayVo, String sign) {
        try {
            return bankPayManager.bankPay(bankPayVo);
        } catch (Exception e) {
            log.error("bankPay:异常 param {}" , bankPayVo, e);
            return ResultVO.createError("系统异常");
        }
    }

    /**
     * 微信支付
     *
     * @param yzhwxPayVo
     * @param sign
     * @return
     */
    @Override
    public ResultVO<YzhwxPayBo> wxPay(YzhwxPayVo yzhwxPayVo, String sign) {
        try {
            return wxPayManager.wxPay(yzhwxPayVo, sign);
        }
        catch(Exception ex) {
            log.error("YunZhangHuApi#wxPay error, param=" + JSONUtils.toJSON(yzhwxPayVo) + ", msg=" + ex.getMessage(), ex);

            if(ex instanceof PaymentException) {
                return ResultVO.createError(((PaymentException) ex).getErrCode(), ((PaymentException) ex).getErrMsg());
            }
            else {
                return ResultVO.createError(ResultCodeEnum.DEFAULT_ERROR);
            }
        }
    }
}
